package com.sdry.basic.controller;

import com.sdry.basic.entity.AttendanceTeam;
import com.sdry.basic.entity.AttendanceTeamType;
import com.sdry.basic.entity.AttendanceTeamUser;
import com.sdry.basic.service.AttendanceService;
import com.sdry.common.BaseController;
import com.sdry.common.TableDataInfo;
import com.sdry.system.entity.SysDept;
import com.sdry.system.entity.SysUser;
import com.sdry.system.service.DeptService;
import com.sdry.untils.ResponseUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author lian
 * @date 2020/04/13
 */
@Controller
@RequestMapping(value = "attendance")
public class AttendanceController extends BaseController {

    @Resource
    private AttendanceService attendanceService;
    @Resource
    private DeptService deptService;

    /**
     * 进班组类型页面
     * @return
     */
    @RequestMapping("/goAttendanceType")
    public String goAttendanceType(){
        return "/td/basis/attendanceType";
    }

    /**
     * 分页查询班组类型
     * @return
     */
    @ApiOperation(value = "获取班组类型列表",notes = "分页查询")
    @ApiImplicitParam(name = "queryAttendanceType",value = "班组类型实体AttendanceType",required = true,dataType = "AttendanceTeamType")
    @RequestMapping(value = "/queryAttendanceType")
    @ResponseBody
    public TableDataInfo queryAttendanceType(AttendanceTeamType att) {
        logger.info("分页查询班组类型 AttendanceTeamType = " + JSONObject.fromObject(att));
        List<AttendanceTeamType> list = attendanceService.queryAttendanceType(att);
        Long count = attendanceService.countAttendanceType(att);
        return getDataTable(list, count);
    }

    /**
     * 查询所有部门
     * @return
     */
    @RequestMapping(value = "/getDeptList")
    @ResponseBody
    public List<SysDept> getDeptList() {
        logger.info("查询所有部门:无参数");
        List<SysDept> list = deptService.list();
        return list;
    }

    /**
     * 查询所有班组
     * @return
     */
    @RequestMapping(value = "/getTeamList")
    @ResponseBody
    public List<AttendanceTeam> getTeamList() {
        logger.info("查询所有班组:无参数");
        List<AttendanceTeam> list = attendanceService.getTeamList();
        return list;
    }

    /**
     * 插入班组类型返回id
     * @param param 实体条件
     * @param response
     */
    @RequestMapping("/insertAttendanceTeamType")
    public void insertAttendanceTeamType(AttendanceTeamType param, HttpServletResponse response){
        logger.info("给班组类型表，插入内容："+ JSONArray.fromObject(param));
        Long id = attendanceService.insertAttendanceType(param);
        logger.info("成功返回id="+id);
        try {
            ResponseUtil.write(response,id);
        } catch (Exception e) {
            logger.info("插入失败");
            e.printStackTrace();
        }
    }

    /**
     * 根据ID修改班组类型
     * @param param 实体条件
     * @param response
     */
    @RequestMapping("/updateAttendanceTeamType")
    public void updateAttendanceTeamType(AttendanceTeamType param, HttpServletResponse response){
        logger.info("修改的内容"+ JSONArray.fromObject(param));
        Long id = attendanceService.updateAttendanceType(param);
        logger.info("修改后返回id"+id);
        try {
            ResponseUtil.write(response,id);
        } catch (Exception e) {
            logger.info("修改数据失败");
            e.printStackTrace();
        }
    }

    /**
     * 根据主键拼接的字符串删除返回影响行数
     * @param ids 主键拼接的字符串
     * @return 影响行数
     */
    @RequestMapping("/deleteAttendanceTeamType")
    public void deleteAttendanceTeamType(String ids, HttpServletResponse response){
        logger.info("根据主键拼接的字符串删班组类型：主键拼接"+ids);
        ids=ids.replace("'","");
        Long count = attendanceService.deleteAttendanceType(ids);
        logger.info("根据主键拼接的字符串删班组类型，返回的影响行数="+count);
        try {
            ResponseUtil.write(response,count);
        } catch (Exception e) {
            logger.info("根据主键拼接的字符串删班组类型，返回失败");
            e.printStackTrace();
        }
    }

    /**
     * 获取未绑定班组的用户
     * @return 返回null.没查到数据
     */
    @ApiOperation(value = "获取未绑定班组的用户",notes = "分页查询")
    @ApiImplicitParam(name = "queryUsersAreNotOnTheTeam",value = "用户实体SysUser",required = true,dataType = "SysUser")
    @RequestMapping(value = "/queryUsersAreNotOnTheTeam")
    @ResponseBody
    public TableDataInfo queryUsersAreNotOnTheTeam(SysUser user) {
        logger.info("查询未绑定班组的用户 SysUser = " + JSONObject.fromObject(user));
        List<SysUser> list = attendanceService.queryUsersAreNotOnTheTeam(user);
        Long count = 1000L;
        return getDataTable(list, count);
    }

    /**
     * 插入班组和用户关系返回id
     * @param ids 用户ID拼接的字符串
     * @param attendanceId 班组类型表的ID
     * @param response 发送
     */
    @RequestMapping("/insertAttendanceTeamUser")
    public void insertAttendanceTeamUser(String ids, Long attendanceId, HttpServletResponse response){
        logger.info("给班组用户表插入内容：班组类型ID="+ attendanceId + ", 用户ID=" +ids);
        AttendanceTeamUser param = new AttendanceTeamUser();
        param.setTeamId(attendanceId);
        param.setDescription(ids);
        Long id = attendanceService.insertAttendanceUser(param);
        logger.info("给班组用户表插入成功，返回id=" +id);
        try {
            ResponseUtil.write(response,id);
        } catch (Exception e) {
            logger.info("给班组用户表插入数据失败");
            e.printStackTrace();
        }
    }

    /**
     * 根据班组ID查询班组的成员
     * @return 返回null.没查到数据
     */
    @ApiOperation(value = "根据班组ID查询班组的成员",notes = "分页查询")
    @ApiImplicitParam(name = "queryTeamMembers",value = "班组ID：teamId",required = true,dataType = "Long")
    @RequestMapping(value = "/queryTeamMembers")
    @ResponseBody
    public TableDataInfo queryTeamMembers(Long teamId) {
        logger.info("根据班组ID查询班组的成员 teamId = " + teamId);
        List<SysUser> list = attendanceService.queryTeamMembers(teamId);
        Long count = 1000L;
        return getDataTable(list, count);
    }


    /**
     * 根据主键拼接的字符串删除班组中的用户
     * @param ids 用户主键拼接的字符串
     * @return 影响行数
     */
    @RequestMapping("/deleteAttendanceTeamUser")
    public void deleteAttendanceTeamUser(String ids, HttpServletResponse response){
        logger.info("根据用户的主键拼接的字符串删除班组中的用户,参数ids="+ids);
        ids=ids.replace("'","");
        Long count = attendanceService.deleteAttendanceTeamUser(ids);
        logger.info("根据用户的主键拼接的字符串删除班组中的用户，返回影响行数="+count);
        try {
            ResponseUtil.write(response,count);
        } catch (Exception e) {
            logger.info("根据用户的主键拼接的字符串删除班组中的用户，删除失败");
            e.printStackTrace();
        }
    }

    /**
     * APP考勤打卡接口
     * @param userId 打卡人
     * @param lon 打卡经度
     * @param lat 打卡维度
     * @param clockType 打卡类型（0:上班，1:下班）
     * @response 给APP的返回值：打卡是否成功
     */
    @RequestMapping("/api/appAttendanceClock")
    @ResponseBody
    public void appAttendanceClock(String userId, String lat, String lon, String clockType, HttpServletResponse response){
        logger.info("APP考勤打卡接口,参数userId="+userId + ",打卡经度lon=" + lon + ",打卡维度lat=" + lat + ",打卡类型clockType=" + clockType);
        JSONObject jsonObject = attendanceService.appAttendanceClock(userId, lat, lon, clockType);
        try {
            ResponseUtil.write(response,jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
